package com.atguigu.gulimail.product.app;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimail.product.entity.AttrEntity;
import com.atguigu.gulimail.product.entity.AttrGroupEntity;
import com.atguigu.gulimail.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimail.product.service.AttrGroupService;
import com.atguigu.gulimail.product.service.AttrService;
import com.atguigu.gulimail.product.service.CategoryService;
import com.atguigu.gulimail.product.vo.AttrGroupDeleteVo;
import com.atguigu.gulimail.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author fangyi
 * @email 2244210628@qq.com@gmail.com
 * @date 2022-08-09 22:57:13
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    //    /225/withattr
    @RequestMapping("/{catelogId}/withattr")
    public R getAttrGroupsByCatId(@PathVariable("catelogId") Long catelogId) {

        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupService.getAttrsBycatLogId(catelogId);

        return R.ok().put("data",attrGroupWithAttrsVos);

    }


    //    /attr/relation
    @RequestMapping("/attr/relation")
    public R saveList(@RequestBody AttrGroupDeleteVo[] params) {

        attrAttrgroupRelationService.saveAttrAndGroupRelation(params);

        return R.ok();

    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    //    /1/noattr/relation
    @RequestMapping("/{attrGroupId}/noattr/relation")
    public R noAttrList(@RequestParam Map<String, Object> params,
                        @PathVariable("attrGroupId") Long attrGroupId) {

        PageUtils page = attrService.queryNoAttrPage(params, attrGroupId);

        return R.ok().put("page", page);
    }


    //    product/attrgroup/5/attr/relation
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R relationList(@PathVariable("attrgroupId") Long attrgroupId) {

        List<AttrEntity> data = attrService.QueryAttrGroupRelation(attrgroupId);

        return R.ok().put("data", data);
    }

    //    attrgroup/attr/relation/delete
    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupDeleteVo[] attrgroupId) {

        attrService.DeleteAttrGroupRelation(attrgroupId);

        return R.ok();
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    public R listByCategoryId(@RequestParam Map<String, Object> params,
                              @PathVariable("categoryId") Long categoryId) {

        PageUtils page = attrGroupService.queryPage(params, categoryId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long id = attrGroup.getCatelogId();

        Long[] path = categoryService.findCategoryPath(id);

        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {


        attrGroupService.removeAttrGroupAndRelation(Arrays.asList(attrGroupIds));


        return R.ok();
    }

}
